package com.millstein.tsinglog.mapper.biz;

import com.millstein.tsinglog.mapper.BaseMapper;
import com.millstein.tsinglog.model.pojo.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface RoleMapper extends BaseMapper<Role> {
    /**
     * 根据id查询角色
     * @param id
     * @return
     */
    @Select("SELECT `id`, `role_name` roleName, `sort`, `description`, `usable`, `status`, " +
            "`create_time` createTime, `update_time` updateTime FROM `sys_role` " +
            "where `status` = 1 and `id` = #{id};")
    Role selectRoleByPrimaryKey(@Param("id") Long id);

    /**
     * 在sys_role_permission中间表中根据roleId查询所有的记录
     * @param roleId 角色id
     * @return
     */
    @Select("select id, role_id, permission_id, status, create_time, update_time from sys_role_permission where " +
            "role_id = #{roleId}")
    List<Map<String, Object>> getMultipleRelations(@Param("roleId") Long roleId);

    /**
     * 在sys_role_permission中间表中根据roleId删除对应的记录
     * @param roleId 角色id
     * @return
     */
    @Delete("delete from sys_role_permission where role_id = #{roleId}")
    int deleteMultipleRelations(@Param("roleId") Long roleId);

    /**
     * 在sys_role_permission中间表中插入数据
     * @param roleId 角色id
     * @param permissionId 权限id
     * @param dateStr 时间
     * @return
     */
    @Insert("insert into sys_role_permission values (null, #{roleId}, #{permissionId}, 1, #{dateStr}, #{dateStr})")
    int insertMultipleRelations(@Param("roleId") Long roleId, @Param("permissionId") Long permissionId,
                                @Param("dateStr") String dateStr);
}
